<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form
              ref="updateForm"
              :model="formData"
              :rules="rules"
              label-width="120px"
              style="margin-left: 120px; margin-top: 30px"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="formData.username" style="width: 300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input
                  v-model="formData.password2"
                  style="width: 300px"
                  type="password"
                />
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  @click="updateEmployeeDetail"
                >更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <UserInfo />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <JobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetail } from '@/api/user'
import { saveEmployeeDetail } from '@/api/employees'
import UserInfo from './components/user-info'
import JobInfo from './components/job-info'
export default {
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      userId: this.$route.params.id,
      formData: {
        username: '',
        password2: ''
      },
      rules: {
        username: [
          { required: true, message: '此项不能为空', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '此项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getEmployeeInfo()
  },
  methods: {
    // 获取员工详细信息处理函数
    async getEmployeeInfo() {
      this.formData = await getUserDetail(this.userId)
    },
    // 点击更新按钮更改用户数据处理函数
    async updateEmployeeDetail() {
      // 表单校验
      await this.$refs.updateForm.validate()
      // 发请求
      await saveEmployeeDetail({
        ...this.formData,
        password: this.formData.password2
      })
      // 提示信息
      this.$message.success('更新成功')
    }
  }
}
</script>

<style>
</style>
